package com.km.team.phsystem.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.km.team.phsystem.beans.pojo.SystemBusitype;
import com.km.team.phsystem.beans.pojo.TimeSlotPhConfig;
import com.km.team.phsystem.beans.vo.*;
import com.km.team.phsystem.common.constants.Constants;
import com.km.team.phsystem.common.enums.ResultEnum;
import com.km.team.phsystem.web.utils.ResponseDataUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.km.team.phsystem.web.controller.base.BaseController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: rochzhu
 * Date: 2018/11/26
 * Time: 19:00pm
 * Description: 业务类型
 */

@RestController
@RequestMapping("/sysbusitype")
public class SysBusiTypeController extends BaseController<SysBusiTypeController> {

    /**
     * 1.添加业务类型和时段
     * @param busiTypeVo
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @ApiOperation(value="新增业务类型", notes="")
    @PostMapping("/add")
    public ResponseData add(@RequestBody @Valid List<BusiTypeVo> busiTypeVo, BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()){
            return ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        //组装业务类型表数据对象
        SystemBusitypeVo systemBusitypeVo = new SystemBusitypeVo();
        systemBusitypeVo.setBusitype(busiTypeVo.get(0).getBusitypename());
        systemBusitypeVo.setBusitype_count(busiTypeVo.get(0).getBusitype_count());
        systemBusitypeVo.setNote(busiTypeVo.get(0).getBusitype_note());
        //重复查询
        List<SystemBusitype> list = iSysBusiTypeService.getSystemBusiType(systemBusitypeVo);
        if (!CollectionUtils.isEmpty(list)){
            return ResponseDataUtil.buildError(Constants.ERROR, "业务类型名称重复！");
        }

        int insertflag = iSysBusiTypeService.add(systemBusitypeVo);//插入业务类型表
        int effectID = 0;
        if(insertflag>0){
            List<SystemBusitype> list_insert = iSysBusiTypeService.getSystemBusiType(systemBusitypeVo);
            effectID = list_insert.get(0).getId();//赋给刚插入的类型ID号
        }else {
            return ResponseDataUtil.buildError(ResultEnum.BUSINESS_ERROR);
        }
        // 去操作业务类型时段表 【东华侧方法】
        List<TimeSlotPhConfig> timeSlotList = new ArrayList<TimeSlotPhConfig>();
        if(busiTypeVo.size()>0){
            for (int i=0;i<busiTypeVo.size();i++){
                TimeSlotPhConfig timeSlotPhConfig = new TimeSlotPhConfig();
                timeSlotPhConfig.setSystemBusitypeId(effectID);
                timeSlotPhConfig.setSystemBusitypeType(busiTypeVo.get(i).getBusitypename());
                timeSlotPhConfig.setAcceptNo(busiTypeVo.get(i).getTime_count());
                timeSlotPhConfig.setStartTime(busiTypeVo.get(i).getTime_start());
                timeSlotPhConfig.setEndTime(busiTypeVo.get(i).getTime_end());
                timeSlotPhConfig.setIsEnable(busiTypeVo.get(i).isTime_enable());
                timeSlotList.add(timeSlotPhConfig);
            }
        }
        if(timeSlotList.size()>0){
            timeSlotPhConfigService.instOrUpdateTimeSlot(timeSlotList,effectID,busiTypeVo.get(0).getBusitype_count());
        }
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 2.更新业务类型和时段
     * @param busiTypeAllVo
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public ResponseData update(@RequestBody List<BusiTypeAllVo> busiTypeAllVo) throws Exception {
        if(busiTypeAllVo==null){
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        //组装业务类型表数据对象
        SystemBusitype systemBusitype = new SystemBusitype();
        systemBusitype.setId(busiTypeAllVo.get(0).getBusitype_id());
        systemBusitype.setBusitype(busiTypeAllVo.get(0).getBusitypename());
        systemBusitype.setBusitypeCount(busiTypeAllVo.get(0).getBusitype_count());
//        systemBusitype.setNote(busiTypeAllVo.get(0).getBusitype_note());
        iSysBusiTypeService.updateBusiType(systemBusitype);

        // 去更新业务类型时段表 【东华的方法】
        List<TimeSlotPhConfig> timeSlotList = new ArrayList<TimeSlotPhConfig>();
        if(busiTypeAllVo.size()>0){
            for (int i=0;i<busiTypeAllVo.size();i++){
                TimeSlotPhConfig timeSlotPhConfig = new TimeSlotPhConfig();
                timeSlotPhConfig.setId(busiTypeAllVo.get(i).getTime_id());
                timeSlotPhConfig.setSystemBusitypeId(busiTypeAllVo.get(0).getBusitype_id());
                timeSlotPhConfig.setSystemBusitypeType(busiTypeAllVo.get(0).getBusitypename());
                timeSlotPhConfig.setAcceptNo(busiTypeAllVo.get(i).getTime_count());
                timeSlotPhConfig.setStartTime(busiTypeAllVo.get(i).getTime_start());
                timeSlotPhConfig.setEndTime(busiTypeAllVo.get(i).getTime_end());
                timeSlotPhConfig.setIsEnable(busiTypeAllVo.get(i).isTime_enable());
                timeSlotList.add(timeSlotPhConfig);
            }
        }
        if(timeSlotList.size()>0){
            timeSlotPhConfigService.instOrUpdateTimeSlot(timeSlotList,busiTypeAllVo.get(0).getBusitype_id(),busiTypeAllVo.get(0).getBusitype_count());
        }
        return ResponseDataUtil.buildSuccess();
    }


    /**
     * 3.查询业务类型【OK】
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/querySysBusiTypeData", produces = "application/json;charset=UTF-8")
    public ResponseData querySysBusiTypeData() throws Exception {
        return ResponseDataUtil.buildSuccess(iSysBusiTypeService.getSysBusiTypeData());
    }

    /**
     * 4.通过ID获取业务类型和时段数据【ok】
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/getbusitypebyid/{id}")
    public ResponseData getbusitypebyid(@PathVariable("id") Integer id) throws Exception {
        final SystemBusitype systemBusitype = iSysBusiTypeService.getSystemBusiTypeByID(id);//获取业务类型
        final List<TimeSlotPhConfig> timeSlotPhConfigs = timeSlotPhConfigService.getTimeSlotInfoForBusiness(id);//获取业务类型时段【东华的方法】
        final List<BusiTypeAllVo> busiTypeAllVos = new ArrayList<BusiTypeAllVo>();
        if(timeSlotPhConfigs.size()>0){
            for(int i=0;i<timeSlotPhConfigs.size();i++){
                BusiTypeAllVo busiTypeAllVo = new BusiTypeAllVo();
                busiTypeAllVo.setBusitype_id(systemBusitype.getId());
                busiTypeAllVo.setBusitypename(systemBusitype.getBusitype());
                busiTypeAllVo.setBusitype_count(systemBusitype.getBusitypeCount());
                busiTypeAllVo.setTime_id(timeSlotPhConfigs.get(i).getId());
                busiTypeAllVo.setTime_start(timeSlotPhConfigs.get(i).getStartTime());
                busiTypeAllVo.setTime_end(timeSlotPhConfigs.get(i).getEndTime());
                busiTypeAllVo.setTime_count(timeSlotPhConfigs.get(i).getAcceptNo());
                busiTypeAllVo.setTime_enable(timeSlotPhConfigs.get(i).getIsEnable());
                busiTypeAllVos.add(busiTypeAllVo);
            }
        }
        return ResponseDataUtil.buildSuccess(busiTypeAllVos);
    }

    /**
     * 5.删除业务类型【OK】
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete/{id}")
    public ResponseData delete(@PathVariable("id") Integer id) throws Exception {
        iSysBusiTypeService.delete(id);//删除业务类型表
        centralizedDateTimeSlotHandleService.centralizedDeleteForBusinessId(id);// 删除时段信息【东华的方法】
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 6.单获取业务类型数据【ok】
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/getbusitypesinglebyid/{id}")
    public ResponseData getbusitypesinglebyid(@PathVariable("id") Integer id) throws Exception{
        SystemBusitype systemBusitype = iSysBusiTypeService.getSystemBusiTypeByID(id);
        return ResponseDataUtil.buildSuccess(systemBusitype);
    }
}
